package temptest;

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;

import org.apache.log4j.Logger;

public class Test {

    private Logger log = Logger.getLogger(this.getClass());

    public static void main(String args[]) {
        new Test().go();
    }

    public void go() {

        String dbHost = "167.3.129.84";
        String dbPort = "12552";
        String dbName = "qadadm";
        String dbUserId = "root";
        String dbPassword = "";
        Connection conn = null;
        Statement s = null;
        ResultSet rs = null;

        String dbURL = "jdbc:datadirect:openedge://" + dbHost + ":" + dbPort
                + ";databaseName=" + dbName;

        /* get database connection */
        try {
            // Class.forName("com.progress.sql.jdbc.JdbcProgressDriver");
            Class.forName("com.ddtek.jdbc.openedge.OpenEdgeDriver");
            conn = DriverManager.getConnection(dbURL, dbUserId, dbPassword);

            /* database meta */
            DatabaseMetaData dbMeta = conn.getMetaData();
            // /* get & print all table */
            // String[] types = { "TABLE" };
            // rs = dbMeta.getTables(null, null, "%", types);
            // log.info("Table name:");
            // while (rs.next()) {
            // String table = rs.getString("TABLE_NAME");
            // log.info(table);
            // }
            {
                rs = dbMeta.getSchemas();
                ResultSetMetaData rsmd = rs.getMetaData();

                int numCols = rsmd.getColumnCount();
                for (int i = 1; i <= numCols; i++) {
                    log.info(rsmd.getColumnTypeName(i) + "-"
                            + rsmd.getColumnClassName(i) + "-"
                            + rsmd.getColumnName(i));
                }
                while (rs.next()) {
                    log.debug(rs.getString(1));
                }
                log.info("---------");
            }
            /* set transaction start */
            conn.setAutoCommit(false);
            s = conn.createStatement();
            {
                rs = dbMeta.getPrimaryKeys(null, "PUB", "brwt_det");
                ResultSetMetaData rsmd = rs.getMetaData();

                int numCols = rsmd.getColumnCount();
                for (int i = 1; i <= numCols; i++) {
                    log.info(i + rsmd.getColumnName(i));
                }
                while (rs.next()) {
                    log.info(rs.getObject(4));
                }
                log.info("---" + rs.toString());
            }
            rs = s.executeQuery("select * from PUB.brwt_det");
            /* Holds the Meta-Data */
            ResultSetMetaData rsmd = rs.getMetaData();

            int numCols = rsmd.getColumnCount();
            for (int i = 1; i <= numCols; i++) {
                log.info(rsmd.getColumnTypeName(i) + "-"
                        + rsmd.getColumnClassName(i) + "-"
                        + rsmd.getColumnName(i));
            }

            // while (rs.next()) {
            //
            // for( int i = 1; i <= rsmd.getColumnCount(); i++ )
            // System.out.print( rs.getString(i) + " " ) ;
            // System.out.println() ;
            //
            //
            // }

        } catch (SQLException e) {
            e.printStackTrace();

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
}
